package com.example.kp.mci;

import java.util.ArrayList;
import java.util.List;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.example.kp.mci.constants.colorTheme;
import com.example.kp.mci.constants.font;

@SuppressLint("DefaultLocale")
public class CvPoiListElementAdapter extends BaseAdapter{

        private List<CvPoiListElement> list ;
        private Context context;
       
        private ArrayList<Poi> items = new ArrayList<Poi>();
       
        private TextView description;
        private TextView value;
       
       
        public CvPoiListElementAdapter(Context context,List<CvPoiListElement> list, ArrayList<Poi> items) {
                this.list = list;
                this.context = context;
                this.items = items;
        }
       
        public int getCount(){
                if (list != null) return  list.size();
                else return 0;
        }

        public CvPoiListElement getItem(int arg0) {
                return list.get(arg0);
        }


        /* (non-Javadoc)
         * @see android.widget.Adapter#getView(int, android.view.View, android.view.ViewGroup)
         */
        
		public View getView(int arg0, View arg1, ViewGroup arg2) {
               
                if (arg1 == null) {
                        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                        arg1 = inflater.inflate(R.layout.cv_poi_list_item,null);
                }
               
                CvPoiListElement cve = list.get(arg0);
  
                if(cve!=null){
                	
                		formatCVElement(arg1);
                       
                        description = (TextView) arg1.findViewById(R.id.cv_description);
                        value       = (TextView) arg1.findViewById(R.id.cv_details);
                        
                       if(PreferencesManager.getInstance().getFont() != font.BLOCK_SANS_SERIF){
	                        description.setText(items.get(arg0).getName());
	                        value.setText(items.get(arg0).getDescription());
                       }else{
                    	   	description.setText(items.get(arg0).getName().toUpperCase());
	                        value.setText(items.get(arg0).getDescription().toUpperCase());
                       }
                       
                }              
               
               
                return arg1;
        }

		public long getItemId(int position) {
			if(items.get(position)!=null)return Integer.parseInt(items.get(position).getId().replace("N", ""));
            return 0;
		}
		
		public String getItemSpeechString(int position){
			if(items.get(position)!=null) return items.get(position).getName()+ ". " + items.get(position).getDescription();
			return "";
		}
		
		public void formatCVElement(View element){
			
			TextView elementDescription = (TextView) element.findViewById(R.id.cv_description);
            TextView elementDetails     = (TextView) element.findViewById(R.id.cv_details);
            
            switch (PreferencesManager.getInstance().getColorTheme()) {
	    		case colorTheme.NORMAL:
	    			elementDescription.setTextColor(Color.BLACK);
	    			elementDescription.setBackgroundColor(Color.WHITE);
	    			elementDetails.setTextColor(Color.BLACK);
	    			elementDetails.setBackgroundColor(Color.WHITE);	
	    			break;
	    		case colorTheme.NEGATIVE:
	    			elementDescription.setTextColor(Color.WHITE);
	    			elementDescription.setBackgroundColor(Color.BLACK);
	    			elementDetails.setTextColor(Color.WHITE);
	    			elementDetails.setBackgroundColor(Color.BLACK);
	    			break;
	    		case colorTheme.HIGH_KONTRAST:
	    			elementDescription.setTextColor(Color.WHITE);
	    			elementDescription.setBackgroundColor(Color.BLACK);
	    			elementDetails.setTextColor(Color.WHITE);
	    			elementDetails.setBackgroundColor(Color.BLACK);
	    			break;
            }
    			
    		switch (PreferencesManager.getInstance().getFont()) {
    			case font.NORMAL_SANS_SERIF:
    				elementDescription.setTypeface(Typeface.SANS_SERIF);
    				elementDetails.setTypeface(Typeface.SANS_SERIF);
    				break;
    			case font.BLOCK_SANS_SERIF:
    				Typeface textFont = Typeface.createFromAsset(context.getAssets(),"Blockschrift.ttf");
    				elementDescription.setTypeface(textFont);
    				elementDetails.setTypeface(textFont);
    				break;
    			case font.SERIF:
    				Typeface textFont2 = Typeface.createFromAsset(context.getAssets(),"serifen.otf");
    				elementDescription.setTypeface(textFont2);
    				elementDetails.setTypeface(textFont2);
    				break;
    		}
    		
    		elementDescription.setTextSize(PreferencesManager.getInstance().getFontSize());
    		elementDetails.setTextSize(PreferencesManager.getInstance().getFontSize()-5);
			
		}
		

}
